package com.talking.activities
import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.JsonParser
import com.parfoismeng.slidebacklib.registerSlideBack
import com.parfoismeng.slidebacklib.unregisterSlideBack
import com.talking.R
import com.talking.databinding.ActivityRegisterBinding
import com.talking.util.Network
import com.talking.util.Tools
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import java.util.ArrayList
import java.util.HashMap

class RegisterActivity : AppCompatActivity() {
    private val calls: MutableList<Call> = ArrayList()
    private lateinit var binding: ActivityRegisterBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setResult(1)
        binding.registerLogo.logo.typeface = Typeface.createFromAsset(assets, "font1.TTF")
        binding.registerRegister.setOnClickListener{
            if (binding.registerMail.text.toString() == ""){
                Tools.toast(getString(R.string.input_mail))
                return@setOnClickListener
            }
            if (!Tools.isEmail(binding.registerMail.text.toString())){
                Tools.toast(getString(R.string.mail_format_error))
                return@setOnClickListener
            }
            if (binding.registerPassword.text.toString()==""){
                Tools.toast(getString(R.string.input_password))
                return@setOnClickListener
            }
            if (!Tools.passwordCorrect(binding.registerPassword.text.toString())){
                Tools.toast(getString(R.string.password_format))
                return@setOnClickListener
            }
            if (binding.registerPassword2.text.toString()==""){
                Tools.toast(getString(R.string.input_password_again))
                return@setOnClickListener
            }
            if (binding.registerPassword.text.toString() != binding.registerPassword2.text.toString()){
                Tools.toast(getString(R.string.password_not_match))
                return@setOnClickListener
            }
            val map = HashMap<String, String>()
            map["mail"] = binding.registerMail.text.toString()
            map["password"] = binding.registerPassword.text.toString()
            Network.post(map, "user/register/") { call: Call ->
                call.enqueue(object : Callback {
                    override fun onFailure(call: Call, e: IOException) {
                        e.printStackTrace()
                    }
                    override fun onResponse(call: Call, response: Response) {
                        runOnUiThread{
                            val msg = JsonParser.parseString(response.body!!.string()).asJsonObject.get("msg").asString
                            if (msg.equals("注册成功")){
                                setResult(3,Intent().putExtra("mail",binding.registerMail.text.toString()).putExtra("password",binding.registerPassword.text.toString()))
                                finish()
                            }else{
                                Tools.toast(msg)
                            }
                        }
                    }
                })
                calls.add(call)
            }
        }
        registerSlideBack { finish() }
    }
    override fun onDestroy() {
        super.onDestroy()
        for (c in calls) c.cancel()
        unregisterSlideBack()
    }
}